<!--
 * @Author: zhaoziyin
 * @Date: 2022-10-12 15:09:04
 * @LastEditTime: 2022-10-16 19:37:04
 * @LastEditors: zhaoziyin
 * @Description: 一直游到海水变蓝
 * @FilePath: \项目d:\桌面\新建文件夹 (2)\home-economics-cloud-service\src\pages\admin\waiter\Details.vue
 * 别乱动！
-->
<template>
  <div>
    <div class="header">
      <div class="pic">
        <img :src="form.userFace" alt="" />
      </div>
      <div><a href="#" @click.prevent="changeFace">更换头像</a></div>
    </div>
    <hr />
    <!-- 第二行 信息 -->
    <div class="msg">
      <ul>
        <li>
          <span>登录用户</span>
          {{ cusDetails.username }}
        </li>
        <li>
          <span>用户姓名</span>
          {{ cusDetails.realname }}
        </li>
        <li>
          <span>用户电话</span>
          {{ cusDetails.telephone }}
        </li>
        <li>
          <span>用户性别</span>
          <span v-if="cusDetails.gender === 'male'">男</span>
          <span v-else>女</span>
        </li>
        <li>
          <span>用户状态</span>
          {{ cusDetails.status }}
        </li>
        <li>
          <span>用户角色</span>
          员工
          <!-- {{ cusDetails.roles[0].name }} -->
        </li>
      </ul>
    </div>
    <!-- 第二行 信息结束 -->
    <!-- 第三行 模态框 -->
    <el-dialog @close="resetForm" :title="title" :visible.sync="visible">
      <el-form ref="myForm" :model="form">
        <el-form-item
          label="上传头像"
          :label-width="formLabelWidth"
          prop="userFace"
        >
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.userFace" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 第三行 模态框结束 -->
  </div>
</template>

<script>
import { queryCustomeById, alterUserface } from "@/api/user";
import { uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      id: 0,
      cusDetails: {},
      title: "修改头像",
      // 图片上传路径
      uploadFileURL,
      // 控制模态框显示与隐藏变量
      visible: false,
      // 头像上传图片变量
      imageUrl:"" ,
      form: {},
      formLabelWidth: "120px",
      faceData: {}
    };
  },
  computed: {},
  methods: {
    // 用id获取用户详情
    async getCustomeById() {
      let res = await queryCustomeById({ id: this.id });
      // console.log(res);
      // let {data} = res;
      this.cusDetails = res.data;
      // console.log(this.cusDetails);
    },
    // 模态框关闭重置表单
    resetForm() {
      this.$refs.myForm.resetFields();
      this.visible=false
    },
    // 更换用户头像
    changeFace() {
      this.visible = true;
    },
    // 点击确定保存头像
    async onSubmit(){
      await alterUserface({...this.userFace,id:this.id})
      // console.log('换头像');
      this.visible=false
    },
    // 图片上传成功的方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.userFace = res.data;
    },
    // 图片上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.id = this.$route.query.id;
    this.getCustomeById();
  },
  mounted() {},
};
</script>
<style scoped lang="scss">
.header {
  display: flex;
  justify-content: space-around;
  .pic {
    border-radius: 50%;
    // background-color: aquamarine;
    img {
      width: 50px;
      height: 50px;
    }
  }
  a {
    color: aquamarine;
    display: inline-block;
    padding-top: 20px;
  }
}
.msg{
  ul{
    li{
      padding: 20px;
      span:nth-child(1){
        font-weight: 700;
      }
    }
  }
}
</style>